
Page({
  data:{
    classes:[],
    classesname:[],
    types:[],
    casIndex: 0,
    casIndex2: 0,
    showModalStatus2:false,
    user:{},
    img:[],
    cousename:'',
    videod:'',
    description:'',
    videoname:'',
    video:'',
    progress:0,
    isup1:false,
    isup2: false,
  },
  onLoad:function(){
    let user = wx.getStorageSync('user');
    this.setData({
      user,
    })
    let _this = this;
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000
    })
    wx.request({
      url: 'https://www.azhouaa.cn/wxs/User/getClassType',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        let arr = [];
        res.data.msg.map(function (type) {
          arr.push(type.classifyname)
        });
        _this.setData({
          types: arr
        })
      }
    })
    wx.request({
      url: `https://www.azhouaa.cn/wxs/User/selteCourse?id=${user.id}`,
      header: {
        'content-type': 'application/json'
      },

      success: function (res) {
        if (res.data.code == 200) {
          console.log('课程', res)
          let arr =[];
          res.data.msg.map(function(c){
            arr.push(c.coursename);
          })
          _this.setData({
            classes: res.data.msg,
            classesname: arr
          })
        }
      },
      complete: function () {
        wx.hideToast();
      }
    })
    
  },
  onShow:function(){

  },
  util: function (currentStatu) {   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长    
      timingFunction: "linear", //线性    
      delay: 0  //0则不延迟    
    });
    this.animation = animation;
    animation.opacity(0).rotateX(-100).step();
    this.setData({
      animationData: animation.export()
    })
    setTimeout(function () {
      animation.opacity(1).rotateX(0).step();
      this.setData({
        animationData: animation
      })

      //关闭    
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200) 
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },
  powerDrawer: function (e) {
    this.util(e.currentTarget.dataset.statu)
  },
  util2: function (currentStatu) {
    var animation = wx.createAnimation({
      duration: 200,  //动画时长    
      timingFunction: "linear", //线性    
      delay: 0  //0则不延迟    
    });
    this.animation = animation;
    animation.opacity(0).rotateX(-100).step();
    this.setData({
      animationData: animation.export()
    })
    setTimeout(function () {
      animation.opacity(1).rotateX(0).step();
      this.setData({
        animationData: animation
      })

      //关闭    
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus2: false
          }
        );
      }
    }.bind(this), 200)
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus2: true
        }
      );
    }
  },
  powerDrawer2: function (e) {
    if (this.data.classes.length > 0){
      this.util2(e.currentTarget.dataset.statu)
    }else{
      let _this=this;
      wx.showModal({
        title: '提示',
        content: '请先创建课程在上传视频',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            _this.util('open')
          } 
        }
      })
    }
    
  },
  bindPickerChange: function (e) {
    this.setData({
      casIndex: e.detail.value
    })
  },
  bindPickerChange2: function (e) {
    this.setData({
      casIndex2: e.detail.value
    })
  },
  upload:function(){
    let _this=this;
    this.setData({
      isup1:true
    })
    wx.showToast({
      title: '上传中',
      icon: 'loading',
      duration: 990000
    });
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        console.log(res)
        _this.setData({
          img: res.tempFilePaths
        })
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete:function(){
        wx.hideToast();
        _this.setData({
          isup1: false
        })
      }
    })
  },
  upload2:function(){
    var _this = this
    this.setData({
      isup2: true
    })
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res)
        _this.setData({
          video: res.tempFilePath
        })
      },
      fail:function(){
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete:function(){
        _this.setData({
          isup2: false
        })
      }
    })
    

    //uploadTask.abort()
  },
  addClass(){
    let _data = this.data;
    let _this = this;
    console.log(_data)
    if (_data.cousename != '' && _data.description != '' && _data.img.length>0 ){
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 990000
      });
      const uploadTask =wx.uploadFile({
        url: `https://www.azhouaa.cn/wxs/User/addCourse`,
        method: 'POST',
        name: 'file',
        filePath: _data.img[0],
        formData: {
          'coursename': _data.cousename,
          'description': _data.description,
          'lecturer': _data.user.id,
          'classify': _data.casIndex
        },
        success: function (res) {
          wx.hideToast();
          if (res.data.code == 200) {
            wx.showToast({
              title: '添加课程成功',
              icon: 'success',
              duration: 1500
            });
          }
          _this.setData({
            img:[],
            progress: 0
          })
        },
        fail: function () {
          wx.hideToast();
          wx.showModal({
            title: '信息',
            content: '添加课程失败',
          })
        },
        complete:function(){
          _this.util('close');
        }
      })
      uploadTask.onProgressUpdate((res) => {
        _this.setData({
          progress: res.progress
        })
        console.log('上传进度', res.progress)
        console.log('已经上传的数据长度', res.totalBytesSent)
        console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '请填写完整的信息',
        showCancel:false
      })
    }
   
  },
  addVideo(){
    let _data = this.data,
        _this = this;
    if (_data.videoname != '' && _data.videod != '' && _data.video != '') {
      console.log(_data)
      wx.showToast({
        title: '上传中',
        icon: 'loading',
        duration: 990000
      });
      const uploadTask = wx.uploadFile({
        url: 'https://www.azhouaa.cn/wxs/User/addVideo',
        method: 'POST',
        name: 'file',
        filePath: _data.video,
        formData: {
          'upid': _data.user.id,
          'title': _data.videoname,
          'description': _data.videod,
          'classid': _data.classes[_data.casIndex2].id
        },
        success: function (res) {
          console.log('成功', res);
          wx.hideToast();
          if (res.data.code == 200) {
            wx.showToast({
              title: '添加视频成功',
              icon: 'success',
              duration: 1500
            });
          }
          _this.setData({
            video:'',
            progress: 0
          })
        },
        fail: function (e) {
          console.log("失败", e)
          wx.hideToast();
          wx.showModal({
            title: '信息',
            content: '添加课程失败',
          })
        },
        complete: function () {
          _this.util2('close')
        }
      })
      uploadTask.onProgressUpdate((res) => {
        _this.setData({
          progress: res.progress
        })
        console.log('上传进度', res.progress)
        console.log('已经上传的数据长度', res.totalBytesSent)
        console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '请填写完整的信息',
        showCancel: false
      })
    }
  },
  setCousename: function (e) {
    this.setData({
      cousename: e.detail.value
    })
  },
  setDescription: function (e) {
    this.setData({
      description: e.detail.value
    })
  },
  setVideoname: function (e) {
    this.setData({
      videoname: e.detail.value
    })
  },
  setVideod: function (e) {
    this.setData({
      videod: e.detail.value
    })
  },
})